﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.BLL;
using System.Data;
using System.Text;

namespace DQYT.HTDZ.Web.Handlers
{
    public partial class RoleMenuHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string resultJson = string.Empty;
            

            string op = Request.QueryString["op"];

            switch (op)
            {
                case "getusermenu":
                    string parentId = Request.QueryString["id"];
                    if (string.IsNullOrEmpty(parentId))
                    {
                        parentId = "0";
                    }
                    BDMenuBLL menuBLL = new BDMenuBLL();
                    //DataTable table = menuBLL.GetMenu(Globals.CurrentUserInfo.JSE_ID);
                    DataTable table = menuBLL.GetMenu(Globals.CurrentUserInfo.JSE_ID, int.Parse(parentId));

                    StringBuilder resultBuilder = new StringBuilder("[");
                    string iconCss = string.Empty;
                    string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\",   \"state\": \"{3}\",\"attributes\":{{\"url\": \"{4}\"}}}},";
                    if (table != null && table.Rows.Count > 0)
                    {
                        foreach (var row in table.AsEnumerable())
                        {
                            resultBuilder.AppendFormat(jsonFormat, row["mnu_id"],
                                row["mnu_mc"], row["mnu_fid"], "closed", row["mnu_url"]);
                        }
                    }
                    if (resultBuilder.Length > 1)
                    {
                        resultBuilder = resultBuilder.Remove(resultBuilder.Length - 1, 1);
                    }
                    resultBuilder.Append("]");
                    resultJson = resultBuilder.ToString();
                    break;
                default:
                    break;
            }

            Response.ContentType = "text/json";
            Response.Write(resultJson);
        }
    }
}